package com.monboo.album.view {

	/**
	 * @author robin for Tudou AlbumPlayer 2010-03-26
	 * @ImageAreaMediator ImageArea Mediator
	 * @email:rman@live.cn
	 * @blog:www.xncat.com
	 */

	import com.gs.TweenLite;
	import com.monboo.album.command.AlbumEvent;
	import com.monboo.album.common.AlbumAction;
	import com.monboo.album.model.AlbumProxy;
	import com.monboo.album.view.component.AlbumText;
	import com.monboo.album.view.component.Animation;
	import com.monboo.album.view.component.MousePlane;
	import com.monboo.album.view.component.Picture;
	import com.monboo.album.view.component.Theme;
	import com.monboo.album.view.component.mask.AnimMask;
	import com.monboo.album.view.component.mask.ThemeMask;
	import com.monboo.album.view.component.mask.TransitionMask;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ImageAreaMediator extends Mediator{
		public static const NAME:String = "ImageAreaMediator"; 
		private var viewObj:AlbumLite;
		private var albumData:Object;
		private var _stage:Stage;
		
		private var imageInfo:AlbumInfoMediator;
		private var albumProxy:AlbumProxy;
		
		private var picture:Picture;
		private var theme:Theme;
		private var animation:Animation;
		private var albumText:AlbumText;	
		private var picPlane:MousePlane;
		private var transitionMask:TransitionMask;
		private var transitionSet:Boolean = false;
		
		private var albumMask:MousePlane;
		private var albumMaskContent:Sprite;
		private var themeMask:ThemeMask;
		private var themeMaskContent:Sprite;
		private var animMask:AnimMask;
		private var animMaskContent:Sprite;
		private var picPlaneContent:Sprite;
		
		private var homePicContent:Sprite;
		private var imageAreaMc:Sprite;
		
		public function ImageAreaMediator(obj:Object){
			super(NAME, obj); 
			viewObj = obj as AlbumLite;
			_stage = viewObj.stage;
			
			albumProxy = facade.retrieveProxy(AlbumProxy.NAME) as AlbumProxy;
			albumData = albumProxy.getData();
			
			imageInfo = facade.retrieveMediator(AlbumInfoMediator.NAME) as AlbumInfoMediator;
			imageAreaMc = new Sprite();
			viewObj.addChild(imageAreaMc);
			
			//大图
			homePicContent = new Sprite();
			imageAreaMc.addChild(homePicContent);
			picture = new Picture(albumData.imgUrlArr,imageInfo);		
			homePicContent.addChild(picture);
			picture.addEventListener(AlbumEvent.PLAY_TRANSITION, playTransFun);
			picture.addEventListener(AlbumEvent.AUTO_PLAY, autoPlay);
			
			albumMask = new MousePlane(imageInfo);
			albumMaskContent = AlbumAction.makeArea(imageInfo.imageWidth,imageInfo.imageHeight);
			albumMask.addChild(albumMaskContent);
			albumMask.changeSize(albumMaskContent);
			
			viewObj.addChild(albumMask);
			homePicContent.mask = albumMask;
			
			
			//transition可能未加载到的情况
			//theme,animation用户可能未设置
			if(transitionMask){			
				var transContent:Sprite = transitionMask.getChildByName("transContent") as Sprite;
				transitionMask.changeSize(transContent);
			}
			//相框
			if(albumData.photoFrameUrl != ""){		
				themeMask = new ThemeMask(imageInfo);
				imageAreaMc.addChild(themeMask);
				themeMaskContent = AlbumAction.makeArea(imageInfo.sourceWidth,imageInfo.sourceHeight);
				themeMask.addChild(themeMaskContent);
				themeMask.changeSize(themeMaskContent);
				
				theme = new Theme(albumData.photoFrameUrl,imageInfo);
				viewObj.addChild(theme);
				theme.mask = themeMask;
				theme.visible = false;
			}	
			//动画
			if(albumData.animationUrl != ""){	
				animMask = new AnimMask(imageInfo);
				imageAreaMc.addChild(animMask);
				animMaskContent = AlbumAction.makeArea(imageInfo.sourceWidth,imageInfo.sourceHeight);
				animMask.addChild(animMaskContent);
				animMask.changeSize(animMaskContent);
				
				animation = new Animation(albumData.animationUrl,imageInfo);
				imageAreaMc.addChild(animation);	
				animation.mask = animMask;
				animation.visible = false;	
			}	
			
			//文字
			albumText = new AlbumText(albumData.textArr,albumData.textProperty,imageInfo);
			imageAreaMc.addChild(albumText);
			if(viewObj.hideText){albumText.visible = false;}
			
			//图片之上的plane按钮，用于作双击全屏和侦测鼠标静止或移动事件
			picPlane = new MousePlane(imageInfo);
			picPlaneContent = AlbumAction.makeArea(imageInfo.imageWidth,imageInfo.imageHeight,0);
			picPlane.addChild(picPlaneContent);
			viewObj.addChild(picPlane);
			
			picPlane.changeSize(picPlaneContent);
			picPlane.buttonMode = true;
			picPlane.doubleClickEnabled = true;
			picPlane.mouseChildren = false;
			picPlane.addEventListener(MouseEvent.DOUBLE_CLICK,fullScreenFunc);	
			
		}
		
		override public function listNotificationInterests():Array 
        {  
            return [
            		 AlbumEvent.THUMB_SHOWIMAGE,
            		 AlbumEvent.RESET_IMAGE,
            		 AlbumEvent.OPEN_EFFECT,
            		 AlbumEvent.CLOSE_EFFECT,
            		 AlbumEvent.NEXT_IMAGE,
            		 AlbumEvent.PRE_IMAGE,
            		 AlbumEvent.FIRST_IMAGE,
            		 AlbumEvent.LAST_IMAGE,
            		 AlbumEvent.CHANGE_UI,
            		 AlbumEvent.RESET_UI,
            		 AlbumEvent.KILL_DELAY_CALL,
            		 AlbumEvent.START_AUTO_PLAY,
            		 AlbumEvent.AUTO_PLAY
            		 ];
        }
		 override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
            {  	
                case AlbumEvent.THUMB_SHOWIMAGE:
                	picture.show(imageInfo.current);
					albumText.doText(imageInfo.current);
                	break;
                case  AlbumEvent.OPEN_EFFECT:
                	openEffect(true);
                	break;
                case  AlbumEvent.CLOSE_EFFECT:
                	openEffect(false);
                	break;
                case AlbumEvent.RESET_IMAGE:
                	picture.scaleX = picture.scaleY = 1;
                	break;
                case AlbumEvent.NEXT_IMAGE:
                	picture.show(imageInfo.current);
					albumText.doText(imageInfo.current);
                	break;
                case AlbumEvent.PRE_IMAGE:
                	picture.show(imageInfo.current);
					albumText.doText(imageInfo.current);
                	break;
                case AlbumEvent.FIRST_IMAGE:
                	if(imageInfo.current !=0){
						picture.show(0);
					}
					albumText.doText(0);
                	break;
                case AlbumEvent.LAST_IMAGE:
                	if(imageInfo.current !=imageInfo.total){
						picture.show(imageInfo.total);
					}
					albumText.doText(imageInfo.total);
                	break;
                
                case AlbumEvent.CHANGE_UI:
                	changeUI();
                	break;
                case AlbumEvent.RESET_UI:
                	resetUI();
                	break;
                case AlbumEvent.KILL_DELAY_CALL:
                	break;
                
                case AlbumEvent.START_AUTO_PLAY:
					imageInfo.current = 0;
					sendNotification(AlbumEvent.RESET_IMAGE);
					
					picture.startShow();
					albumText.doText(imageInfo.current);
                	break; 
                case AlbumEvent.AUTO_PLAY:
					autoPlay(null);
                	break;           
            }
        }

		public function getText(n:int):String{
			return albumText.getText(n);
		}
		 
		private function changeUI():void{
			albumText.textFormat();	
			albumMask.changeSize(albumMaskContent);
			
			picture.toChangeSize();
			
			picPlane.changeSize(picPlaneContent);
			
			if(transitionMask){			
				var transContent:Sprite = transitionMask.getChildByName("transContent") as Sprite;	
				transitionMask.changeSize(transContent);
			}
			if(theme){
				var themeContent:Sprite = theme.getChildByName("themeContent") as Sprite;
				themeMask.changeSize(themeMaskContent);
				theme.changeSize(themeContent);
			}
			if(animation){
				var animationContent:Sprite = animation.getChildByName("animationContent") as Sprite;
				animMask.changeSize(animMaskContent);
				animation.changeSize(animationContent);
			}
		}
		private function resetUI():void{
			albumText.textFormat();	
			albumMask.changeSize(albumMaskContent);
			
			picture.toChangeSize();
			
			picPlane.changeSize(picPlaneContent);
			
			if(transitionMask){
				var transContent:Sprite = transitionMask.getChildByName("transContent") as Sprite;
				transitionMask.changeSize(transContent);
			}	
			if(theme){
				var themeContent:Sprite = theme.getChildByName("themeContent") as Sprite;
				themeMask.changeSize(themeMaskContent);
				theme.changeSize(themeContent);
			}	
			if(animation){
				var animationContent:Sprite = animation.getChildByName("animationContent") as Sprite;
				animMask.changeSize(animMaskContent);
				animation.changeSize(animationContent);
			}	
		}
		
		private function openEffect(state:Boolean):void{
			if(state){
				albumText.visible = true;
				
				if(theme){
					theme.visible = true;
				}
				if(animation){
					animation.visible = true;
				}	
				if(transitionMask){
					transitionMask.visible = true;
					picture.mask = transitionMask;
				}
			}else{
				albumText.visible = false;
				
				if(theme){
					theme.visible = false;
				}
				if(animation){
					animation.visible = false;
				}
				if(transitionMask){
					transitionMask.visible = false;
					picture.mask = null;
				}
			}
		}
		
		//play transition
		private function playTransFun(e:Event):void{
			if(viewObj.getTransitionMask() != null && !transitionSet){
        		transitionSet = true;
        		setTransitionMask();
        	}
			if(transitionMask){transitionMask.getTrans(imageInfo.current);}
		}
		private function setTransitionMask():void{
			//转场蒙板
			var arr:Array = viewObj.getTransitionMask().arr;
			var tranSwf:Object = viewObj.getTransitionMask().tranSwf;
			var tMask:TransitionMask = new TransitionMask(arr,tranSwf,imageInfo);
			tMask.name = "transition";
			tMask.visible = false;
			imageAreaMc.addChild(tMask);
			
			picture.mask = tMask;
			transitionMask = tMask;
			trace("[Set Transition Successfully!]");
		}
		
		//fullscreen
		private function fullScreenFunc(e:Event):void{
			sendNotification(AlbumEvent.FULL_SCREEN);
		}
		
		
		private function autoPlay(e:Event):void{
			imageInfo.current = getAutoImage();
			picture.show(imageInfo.current,"slow");
			
			albumText.doText(imageInfo.current);
			sendNotification(AlbumEvent.AUTO_ING,imageInfo.current);
			sendNotification(AlbumEvent.RESET_IMAGE);
		}
		private function getAutoImage():int{
			var imageNum:Number;
			if(!AlbumAction.pageState("next",imageInfo.current,imageInfo.total)){
				imageNum = imageInfo.current+1;
			}else{
				imageNum = 0;
			}
			return imageNum;
		}
	}
}
